<?php namespace app\home\controller;
use system\model\Category;
class ListPage extends Common{
    //动作
    public function index($cid=null,$tid=null){
       //网站标题
	   View::with('cid',$cid);
	   $headConf = ['title'=>'个人博客--列表页'];
	   View::with('headConf',$headConf);
	   if($cid){
		  //说明点击分类过来的
		  //1.获取当前分类所有子集数据（包含子集）
		  $cids = (new Category())->getSon(Db::table('category')->get(),$cid);
		  $cids[] = $cid;//将自己追加进去
		  //p($cids);
		  //2.标签： 分类或者标签名称
		  $header = [
			  'name'=>'分类',
			  'value'=>Db::table('category')->where('cid',$cid)->pluck('cname'),
			  'count'=>Db::table('article')->whereIn('category_cid',$cids)->count(),
		  ];
		  $articleData = Db::table('article')
			  ->join('category','category_cid','=','cid')
			  ->whereIn('category_cid',$cids)
			  ->field('aid,title,sendtime,author,digest,thumb,cid,cname')
			  ->get();
		  foreach($articleData as $k=>$v)
		  {
			 $articleData[$k]['tag'] = Db::table('tag')
				 ->join('article_tag','tid','=','tag_tid')
				 ->where('article_aid',$v['aid'])->lists('tid,tname');
		  }
		  //p($articleData);
	   }
	   if($tid)
	   {//说明点击标签过来的
		  $header = [
			  'name'=>'标签',
			  'value'=>Db::table('tag')->where('tid',$tid)->pluck('tname'),
			  'count'=>Db::table('article_tag')->where('tag_tid',$tid)->count(),
		  ];
		  $articleData = Db::table('category')
			  ->join('article','cid','=','category_cid')
			  ->join('article_tag','aid','=','article_aid')
			  ->where('tag_tid',$tid)->get();
		  foreach($articleData as $k=>$v)
		  {
			 $articleData[$k]['tag']=Db::table('tag')
				 ->join('article_tag','tid','=','tag_tid')
				 ->where('article_aid',$v['aid'])->lists('tid,tname');

		  }
		  //p($articleData);
	   }
	   View::with('articleData',$articleData);
	   View::with('header',$header);
	   return view();
    }
}
